package com.frxuexi.cart.view

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.cart.R
import com.frxuexi.cart.databinding.ActivityCommentAddBinding
import com.frxuexi.cart.viewmodel.CartViewModel
import com.frxuexi.lib_base.entity.CommentAddReqBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.utils.GlideUtils
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/cart/CommentAddActivity")
class CommentAddActivity : BaseActivity<ActivityCommentAddBinding,CartViewModel>() {
    @JvmField
    @Autowired
    var id=-1

    @Autowired
    lateinit var no:String

    var pic=""
    override fun initData() {
        vm.addCommentData.observe(this){
            ToastUtils.showLong("发表成功")
            finish()
        }

    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.serviceCommit.setOnClickListener {
            val shopScore = binding.shopCore.rating.toInt()
            val serviceScore = binding.serviceCore.rating.toInt()
            val content = binding.repContent.text.toString()
            val bean = CommentAddReqBean(
                "",
                content,
                SPUtils.getInstance().getString(Const.PHONE, ""),
                no,
                pic,
                id,
                shopScore,
                serviceScore,
                id.toString(),
                id.toString(),
                SPUtils.getInstance().getInt(Const.UID, -1)
            )
            vm.addComment(bean)
        }

        binding.repIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            pic=bean?.path!!
            GlideUtils.getRoundImage(binding.repIv,pic)
        }
    }

}